package cn.itcat.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
	
	private String sendAreaInfo;
	private String recAreaInfo;
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}



	@Action(value="order_add",results= {@Result(name="success",location="index.html")})
	public String add(){
		String[] sendAreaStr = sendAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setProvince(sendAreaStr[0]);
		sendArea.setCity(sendAreaStr[1]);
		sendArea.setDistrict(sendAreaStr[2]);
		
		String[] recAreaStr = recAreaInfo.split("/");
		Area recArea = new Area();
		recArea.setProvince(recAreaStr[0]);
		recArea.setCity(recAreaStr[1]);
		recArea.setDistrict(recAreaStr[2]);
		
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		
		//调用webService，将数据传递给Bos_managemennts，完成自动分单等逻辑功能
		WebClient.create(Constants.BOS_MANAGEMENTS_URL+"/services/orderSerivce/order")
					.type(MediaType.APPLICATION_JSON).post(model);
		
		return SUCCESS;
	}
}
